<?php

namespace app\controller;

use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Response;

class BaseController
{
    protected $request;

    public function __construct(Request $request = null)
    {
        $this->request = $request;
    }

    public function requestInfo(Request $request = null)
    {
        $request = $request ?: $this->request;

        if ($request) {
            $data = [
                'URI'     => $request->uri(),
                'Path'    => $request->path(),
                'Query'   => $request->queryString(),
                'Method'  => $request->method(),
                'Time'    => microtime(true),
                'GET'     => $request->get(),
                'POST'    => $request->post(),
                'RawBody' => $request->rawBody(),
                'SID'     => $request->sessionId(),
                'Cookie'  => $request->cookie(),
                'Headers' => $request->header(), // ["user-agent": "xxx", "cookie": "PHPSID=39f24c20aa54d841131eeb6a"]
            ];
        }
        else {
            $uri = $_SERVER["REQUEST_URI"] ?? '';
            $data = [
                'URI'     => $uri,
                'Path'    => strstr($uri, '?', true) ?: $uri,
                'Query'   => ltrim(strstr($uri, '?'), '?'),
                'Method'  => $_SERVER["REQUEST_METHOD"] ?? '',
                'Time'    => microtime(true),
                'GET'     => $_GET,
                'POST'    => $_POST,
                'RawBody' => file_get_contents('php://input'),
                'SID'     => $_COOKIE['PHPSID'] ?? '',
                'Cookie'  => $_COOKIE,
                'Headers' => headers(), // ["User-Agent": "xxx", "Cookie": "PHPSID=39f24c20aa54d841131eeb6a"]
            ];
        }

        return $data;
    }
}
